package middleware

import (
	"benkendSystem/helper"
	"github.com/gin-gonic/gin"
)

// LoginAuthCheck 登录认证
func LoginAuthCheck() gin.HandlerFunc {
	return func(c *gin.Context) {
		getToken := c.GetHeader("XT-token")
		if getToken == "" {
			c.AbortWithStatusJSON(401, gin.H{
				"code": 401,
				"msg":  "未认证授权",
			})
			return
		}
		userClaim, err := helper.ParseToken(c.GetHeader("XT-token"))
		if err != nil {
			c.Abort()
			c.AbortWithStatusJSON(401, gin.H{
				"code": 401,
				"msg":  "登录过期，请重新登录",
			})
			return
		} else {
			c.Set("claims", userClaim)
			c.Next()
		}

	}
}
